package com.cat.dataStructure02;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/reconstruct-original-digits-from-english/
 * @create 2025/10/21 19:20
 * @since JDK17
 */

public class Solution37 {
    public String originalDigits(String s) {
        String[] numberWords = {"zero","six","eight","two","seven","five","four","three","one","nine"};
        int[] cnt = new int[128], h = new int[10], seq = new int[]{0,6,8,2,7,5,4,3,1,9};
        List<String> ans = new ArrayList<>();
        for (char c : s.toCharArray()) {
            cnt[c]++;
        }
        for (int i = 0; i < 10; i++) {
            int min = Integer.MAX_VALUE;
            for (int c : numberWords[i].toCharArray()) {
                min = Math.min(min, cnt[c]);
            }
            for (int c : numberWords[i].toCharArray()) {
                cnt[c] -= min;
            }
            h[i] = min;
        }
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < h[i]; j++) {
                ans.add(String.valueOf(seq[i]));
            }
        }
        Collections.sort(ans);
        return ans.stream().collect(Collectors.joining(""));
    }
}
